import { faker } from '@faker-js/faker';
import { randomInt } from 'crypto';
import { eventHandler } from 'h3';

const formatterCN = new Intl.DateTimeFormat('zh-CN', {
  timeZone: 'Asia/Shanghai',
  year: 'numeric',
  month: '2-digit',
  day: '2-digit',
  hour: '2-digit',
  minute: '2-digit',
  second: '2-digit',
});

function generateMockDataList(count: number) {
  const dataList = [];

  for (let i = 1; i <= count; i++) {
    let recordCount = randomInt(11);
    let seq = randomInt(11);

    const dataItem: Record<string, any> = {
      dictId: i,
      dictCode: 'dictCode-' + i,
      dictName: 'dictName-' + i,
      dictTypeCode: 'code' + recordCount,
      dictTypeName: '字典分类-' + recordCount,
      table: 'tb_test_' + i,
      pDictCode: null,
      pDictName: null,
      seq: seq,
      memo: faker.lorem.sentence(),
      status: faker.helpers.arrayElement([0, 1]),
      createdTime: formatterCN.format(
        faker.date.between({ from: '2021-01-01', to: '2024-12-31' }),
      ),
      createdBy: 'admin',
      lastUpdateTime: formatterCN.format(
        faker.date.between({ from: '2021-01-01', to: '2024-12-31' }),
      ),
      lastUpdateBy: 'admin',
    };

    dataList.push(dataItem);
  }

  return dataList;
}

const mockData = generateMockDataList(200);

export default eventHandler(async (event) => {
  const listData = structuredClone(mockData);
  const query = await readBody(event);
  console.log('查询字典数据条件', JSON.stringify(query));
  if (query) {
    if (query.dictTypeCode) {
      let res = listData.filter((item) => {
        let match = item.dictTypeCode === query.dictTypeCode;

        return match;
      });
      // let res = listData;
      return usePageResponseSuccess('1', res.length, res);
    }
  }
  let time = randomInt(5);
  await sleep(time * 1000);
  // return useResponseSuccess(listData);
  return usePageResponseSuccess('1', '10', listData);
});
